# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=peass
pkgver=20251115.74c9337c.r0.g80318c5
_pkgver=20250701-bdcab634
pkgrel=1
pkgdesc='Privilege Escalation Awesome Scripts SUITE (with colors).'
arch=('any')
groups=('blackarch' 'blackarch-scanner')
url='https://github.com/carlospolop/PEASS-ng'
license=('MIT')
depends=('bash')
makedepends=('git' 'python-requests' 'python-yaml' 'wget')
optdepends=('fping' 'nmap' 'ping' 'openbsd-netcat')
source=("$pkgname::git+https://github.com/carlospolop/PEASS-ng.git")
sha512sums=('SKIP')

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

build() {
  cd $pkgname

  # build linpeas.sh and linpeas_fat.sh
  cd linPEAS
  python3 -m builder.linpeas_builder --all --output linpeas_fat.sh
  python3 -m builder.linpeas_builder --all-no-fat --output linpeas.sh

  # build linpeas binaries
  # requires to package sh2bin
  # like in https://github.com/carlospolop/PEASS-ng/blob/e29c9e88d5dc5c33682fd2aeb36f3cae60d3fa21/.github/workflows/CI-master_tests.yml#L178-L190
  # in the meantime use pre-built binaries
  github_baseurl="https://github.com/carlospolop/PEASS-ng/releases/download/$_pkgver"
  declare -a \
    lin_bins=("linpeas_darwin_amd64" "linpeas_darwin_arm64" "linpeas_linux_386"
              "linpeas_linux_amd64" "linpeas_linux_arm" "linpeas_linux_arm64")
  for bin in "${lin_bins[@]}"; do
    wget "$github_baseurl/$bin"
  done

  # build winPEAS.exe
  # mono-msbuild won't build it
  # it requires a windows host as in https://github.com/carlospolop/PEASS-ng/blob/e29c9e88d5dc5c33682fd2aeb36f3cae60d3fa21/.github/workflows/CI-master_tests.yml#L38-L56
  # so download pre-built binaries instead
  cd ../winPEAS/winPEASexe/binaries/
  declare -a \
    win_bins=("winPEASany.exe" "winPEASany_ofs.exe" "winPEASx64.exe"
              "winPEASx64_ofs.exe" "winPEASx86.exe" "winPEASx86_ofs.exe")
  for bin in "${win_bins[@]}"; do
    wget "$github_baseurl/$bin"
  done

  # build winPEAS.bat
  # winPEAS.bat is already built and included in the repo
}

package() {
  cd $pkgname

  install -dm 755 "$pkgdir/usr/bin"
  install -dm 755 "$pkgdir/usr/share/$pkgname"
  install -dm 755 "$pkgdir/usr/share/windows/$pkgname"

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md
  install -Dm 644 -t "$pkgdir/usr/share/licenses/$pkgname/" LICENSE

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/linPEAS/" \
    linPEAS/README.md

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/winPEAS/" \
    winPEAS/README.md

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/winPEAS/winPEASbat/" \
    winPEAS/winPEASbat/README.md

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/winPEAS/winPEASexe/" \
    winPEAS/winPEASexe/README.md

  cp -a --no-preserve=ownership linPEAS/images/ \
    "$pkgdir/usr/share/doc/$pkgname/linPEAS/"
  cp -a --no-preserve=ownership winPEAS/winPEASexe/images/ \
    "$pkgdir/usr/share/doc/$pkgname/winPEAS/winPEASexe/"

  rm -rf linPEAS/{*.md,images,builder} winPEAS/*.md winPEAS/winPEASexe/*.md \
    winPEAS/winPEASbat/*.md

  rm -rf winPEAS/winPEASexe/binaries/{x64,x86,Release,'Obfuscated Releases'}

  cp -a --no-preserve=ownership linPEAS/ "$pkgdir/usr/share/$pkgname/"
  cp -aT --no-preserve=ownership winPEAS/winPEASexe/binaries/ \
    "$pkgdir/usr/share/windows/$pkgname/"
  cp -a --no-preserve=ownership winPEAS/winPEASbat/winPEAS.bat \
    "$pkgdir/usr/share/windows/$pkgname/"

  install -Dm 755 linPEAS/linpeas.sh "$pkgdir/usr/share/peass/linPEAS/linpeas.sh"
  install -Dm 755 linPEAS/linpeas_fat.sh "$pkgdir/usr/share/peass/linPEAS/linpeas_fat.sh"

  ln -sf /usr/share/peass/linPEAS/linpeas.sh "$pkgdir/usr/bin/linpeas"
  ln -sf /usr/share/peass/linPEAS/linpeas_fat.sh "$pkgdir/usr/bin/linpeas_fat"
}
